---
title: Queries
---

import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';

The [CRUDResolver](./resolvers.mdx) automatically exposes two query endpoints. The endpoints names will be derived from
name provided to `@ObjectType` or the class name.

The following examples are based on the following `TodoItemDTO`

```ts title="todo-item.dto.ts"
import { FilterableField } from '@nestjs-query/query-graphql';
import { ObjectType, ID, GraphQLISODateTime } from '@nestjs/graphql';

@ObjectType('TodoItem')
export class TodoItemDTO {
  @IDField(() => ID)
  id!: string;

  @FilterableField()
  title!: string;

  @FilterableField()
  completed!: boolean;

  @FilterableField(() => GraphQLISODateTime)
  created!: Date;

  @FilterableField(() => GraphQLISODateTime)
  updated!: Date;
}

```

In the following examples you will see two endpoints referenced

* `todoItem` - graphql endpoint to find a record by id.
* `todoItems` - graphql endpoint to filter, page, and sort records,

## Find By Id

The following example finds a `TodoItem` by id.

<Tabs
  defaultValue="graphql"
  values={[
    { label: 'GraphQL', value: 'graphql', },
    { label: 'Response', value: 'response', },
  ]
}>
<TabItem value="graphql">

```graphql
{
  todoItem(id: "1") {
    id
    title
    completed
    created
    updated
  }
}

```


</TabItem>
<TabItem value="response">


```json
{
  "data": {
    "todoItem": {
      "id": "1",
      "title": "Create One Todo Item",
      "completed": false,
      "created": "2020-01-14T07:00:31.763Z",
      "updated": "2020-01-14T07:00:31.763Z"
    }
  }
}
```

</TabItem>
</Tabs>

---

## Querying

As described above the `CRUDResolver` will expose a query method called `todoItems`. The result will be either
a [`connection`](https://relay.dev/graphql/connections.htm) or array depending on the [paging strategy you choose](./paging.mdx)

:::note
All examples below assume that a connection is returned but filtering and sorting work the same way for all paging
strategies. To read how to page collections read the [paging docs](./paging.mdx)
:::

By default if you do not provided an arguments you can query for all records.

<Tabs
  defaultValue="graphql"
  values={[
    { label: 'GraphQL', value: 'graphql', },
    { label: 'Response', value: 'response', },
  ]
}>
<TabItem value="graphql">

```graphql
{
  todoItems{
    pageInfo{
      hasNextPage
      hasPreviousPage
      startCursor
      endCursor
    }
    edges{
      node{
        id
        title
        completed
        created
        updated
      }
      cursor
    }
  }
}
```


</TabItem>
<TabItem value="response">


```json
{
  "data": {
    "todoItems": {
      "pageInfo": {
        "hasNextPage": false,
        "hasPreviousPage": false,
        "startCursor": "YXJyYXljb25uZWN0aW9uOjA=",
        "endCursor": "YXJyYXljb25uZWN0aW9uOjI="
      },
      "edges": [
        {
          "node": {
            "id": "1",
            "title": "Create One Todo Item",
            "completed": false,
            "created": "2020-01-01T00:43:16.000Z",
            "updated": "2020-01-01T00:43:16.000Z"
          },
          "cursor": "YXJyYXljb25uZWN0aW9uOjA="
        },
        {
          "node": {
            "id": "2",
            "title": "Create Many Todo Items - 1",
            "completed": false,
            "created": "2020-01-01T00:49:01.000Z",
            "updated": "2020-01-01T00:49:01.000Z"
          },
          "cursor": "YXJyYXljb25uZWN0aW9uOjE="
        },
        {
          "node": {
            "id": "3",
            "title": "Create Many Todo Items - 2",
            "completed": true,
            "created": "2020-01-01T00:49:01.000Z",
            "updated": "2020-01-01T00:49:01.000Z"
          },
          "cursor": "YXJyYXljb25uZWN0aW9uOjI="
        }
      ]
    }
  }
}
```

</TabItem>
</Tabs>

---

### Filtering

Filtering in `query-graphql` has has an object based syntax

For a full reference of filter operations [see filter reference](../concepts/queries.mdx#filter-reference)

The following example filters for all todoItems that are marked completed.

<Tabs
  defaultValue="graphql"
  values={[
    { label: 'GraphQL', value: 'graphql', },
    { label: 'Response', value: 'response', },
  ]
}>
<TabItem value="graphql">

```graphql
{
  todoItems(filter: {completed: {is: true}}){
    pageInfo{
      hasNextPage
      hasPreviousPage
      startCursor
      endCursor
    }
    edges{
      node{
        id
        title
        completed
        created
        updated
      }
      cursor
    }
  }
}
```

</TabItem>
<TabItem value="response">

```json
{
  "data": {
    "todoItems": {
      "pageInfo": {
        "hasNextPage": false,
        "hasPreviousPage": false,
        "startCursor": "YXJyYXljb25uZWN0aW9uOjA=",
        "endCursor": "YXJyYXljb25uZWN0aW9uOjE="
      },
      "edges": [
        {
          "node": {
            "id": "3",
            "title": "Create Many Todo Items - 2",
            "completed": true,
            "created": "2020-01-14T07:00:34.111Z",
            "updated": "2020-01-14T07:00:34.111Z"
          },
          "cursor": "YXJyYXljb25uZWN0aW9uOjA="
        },
        {
          "node": {
            "id": "5",
            "title": "Create Many Todo Items - 4",
            "completed": true,
            "created": "2020-01-14T07:01:27.805Z",
            "updated": "2020-01-14T07:01:27.805Z"
          },
          "cursor": "YXJyYXljb25uZWN0aW9uOjE="
        }
      ]
    }
  }
}
```

</TabItem>
</Tabs>

### Sorting

You can sort by one or more fields by using the `sorting` parameter.

The `sorting` parameter is an array where each item has the following options.

* `field!` - The name of the field to sort by.
* `direction!` - The direction to sort either `ASC` or `DESC`.
* `nulls?` - Optional field to set nulls sort order `NULLS_FIRST` or `NULLS_last`

In this example we sort by title descending.

<Tabs
  defaultValue="graphql"
  values={[
    { label: 'GraphQL', value: 'graphql', },
    { label: 'Response', value: 'response', },
  ]
}>
<TabItem value="graphql">

```graphql
{
  todoItems(sorting: [{ field: title, direction: DESC }]) {
    pageInfo {
      hasNextPage
      hasPreviousPage
      startCursor
      endCursor
    }
    edges {
      node {
        id
        title
        completed
        created
        updated
      }
      cursor
    }
  }
}

```

</TabItem>
<TabItem value="response">

```json
{
  "data": {
    "todoItems": {
      "pageInfo": {
        "hasNextPage": false,
        "hasPreviousPage": false,
        "startCursor": "YXJyYXljb25uZWN0aW9uOjA=",
        "endCursor": "YXJyYXljb25uZWN0aW9uOjQ="
      },
      "edges": [
        {
          "node": {
            "id": "1",
            "title": "Create One Todo Item",
            "completed": false,
            "created": "2020-01-14T07:00:31.763Z",
            "updated": "2020-01-14T07:00:31.763Z"
          },
          "cursor": "YXJyYXljb25uZWN0aW9uOjA="
        },
        {
          "node": {
            "id": "5",
            "title": "Create Many Todo Items - 4",
            "completed": true,
            "created": "2020-01-14T07:01:27.805Z",
            "updated": "2020-01-14T07:01:27.805Z"
          },
          "cursor": "YXJyYXljb25uZWN0aW9uOjE="
        },
        {
          "node": {
            "id": "4",
            "title": "Create Many Todo Items - 3",
            "completed": false,
            "created": "2020-01-14T07:01:27.805Z",
            "updated": "2020-01-14T07:01:27.805Z"
          },
          "cursor": "YXJyYXljb25uZWN0aW9uOjI="
        },
        {
          "node": {
            "id": "3",
            "title": "Create Many Todo Items - 2",
            "completed": true,
            "created": "2020-01-14T07:00:34.111Z",
            "updated": "2020-01-14T07:00:34.111Z"
          },
          "cursor": "YXJyYXljb25uZWN0aW9uOjM="
        },
        {
          "node": {
            "id": "2",
            "title": "Create Many Todo Items - 1",
            "completed": false,
            "created": "2020-01-14T07:00:34.111Z",
            "updated": "2020-01-14T07:00:34.111Z"
          },
          "cursor": "YXJyYXljb25uZWN0aW9uOjQ="
        }
      ]
    }
  }
}
```
</TabItem>
</Tabs>

In this example we sort by completed and title.

<Tabs
  defaultValue="graphql"
  values={[
    { label: 'GraphQL', value: 'graphql', },
    { label: 'Response', value: 'response', },
  ]
}>
<TabItem value="graphql">

```graphql
{
  todoItems(
    sorting: [
      { field: completed, direction: ASC }
      { field: title, direction: DESC }
    ]
  ) {
    pageInfo {
      hasNextPage
      hasPreviousPage
      startCursor
      endCursor
    }
    edges {
      node {
        id
        title
        completed
        created
        updated
      }
      cursor
    }
  }
}
```

</TabItem>
<TabItem value="response">

```json
{
  "data": {
    "todoItems": {
      "pageInfo": {
        "hasNextPage": false,
        "hasPreviousPage": false,
        "startCursor": "YXJyYXljb25uZWN0aW9uOjA=",
        "endCursor": "YXJyYXljb25uZWN0aW9uOjQ="
      },
      "edges": [
        {
          "node": {
            "id": "1",
            "title": "Create One Todo Item",
            "completed": false,
            "created": "2020-01-14T07:00:31.763Z",
            "updated": "2020-01-14T07:00:31.763Z"
          },
          "cursor": "YXJyYXljb25uZWN0aW9uOjA="
        },
        {
          "node": {
            "id": "4",
            "title": "Create Many Todo Items - 3",
            "completed": false,
            "created": "2020-01-14T07:01:27.805Z",
            "updated": "2020-01-14T07:01:27.805Z"
          },
          "cursor": "YXJyYXljb25uZWN0aW9uOjE="
        },
        {
          "node": {
            "id": "2",
            "title": "Create Many Todo Items - 1",
            "completed": false,
            "created": "2020-01-14T07:00:34.111Z",
            "updated": "2020-01-14T07:00:34.111Z"
          },
          "cursor": "YXJyYXljb25uZWN0aW9uOjI="
        },
        {
          "node": {
            "id": "5",
            "title": "Create Many Todo Items - 4",
            "completed": true,
            "created": "2020-01-14T07:01:27.805Z",
            "updated": "2020-01-14T07:01:27.805Z"
          },
          "cursor": "YXJyYXljb25uZWN0aW9uOjM="
        },
        {
          "node": {
            "id": "3",
            "title": "Create Many Todo Items - 2",
            "completed": true,
            "created": "2020-01-14T07:00:34.111Z",
            "updated": "2020-01-14T07:00:34.111Z"
          },
          "cursor": "YXJyYXljb25uZWN0aW9uOjQ="
        }
      ]
    }
  }
}
```
</TabItem>
</Tabs>
